{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of species : 49.\n",
      "Number of reactions : 330.\n",
      "49 330\n"
     ]
    },
    {
     "ename": "ImportError",
     "evalue": "attempted relative import with no known parent package",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb 单元格 1\u001b[0m line \u001b[0;36m<cell line: 46>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     <a href='vscode-notebook-cell://ssh-remote%2B7b22686f73744e616d65223a22457572656b6131307368656e227d/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=36'>37</a>\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39mlen\u001b[39m(species), \u001b[39mlen\u001b[39m(reactions))\n\u001b[1;32m     <a href='vscode-notebook-cell://ssh-remote%2B7b22686f73744e616d65223a22457572656b6131307368656e227d/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=38'>39</a>\u001b[0m reduced_gas \u001b[39m=\u001b[39m ct\u001b[39m.\u001b[39mSolution(\n\u001b[1;32m     <a href='vscode-notebook-cell://ssh-remote%2B7b22686f73744e616d65223a22457572656b6131307368656e227d/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=39'>40</a>\u001b[0m     thermo\u001b[39m=\u001b[39m\u001b[39m'\u001b[39m\u001b[39mideal-gas\u001b[39m\u001b[39m'\u001b[39m, \n\u001b[1;32m     <a href='vscode-notebook-cell://ssh-remote%2B7b22686f73744e616d65223a22457572656b6131307368656e227d/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=40'>41</a>\u001b[0m     kinetics\u001b[39m=\u001b[39m\u001b[39m'\u001b[39m\u001b[39mgas\u001b[39m\u001b[39m'\u001b[39m,\n\u001b[1;32m     <a href='vscode-notebook-cell://ssh-remote%2B7b22686f73744e616d65223a22457572656b6131307368656e227d/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=41'>42</a>\u001b[0m     species\u001b[39m=\u001b[39mspecies, \n\u001b[1;32m     <a href='vscode-notebook-cell://ssh-remote%2B7b22686f73744e616d65223a22457572656b6131307368656e227d/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=42'>43</a>\u001b[0m     reactions\u001b[39m=\u001b[39mreactions,\n\u001b[1;32m     <a href='vscode-notebook-cell://ssh-remote%2B7b22686f73744e616d65223a22457572656b6131307368656e227d/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=43'>44</a>\u001b[0m )\n\u001b[0;32m---> <a href='vscode-notebook-cell://ssh-remote%2B7b22686f73744e616d65223a22457572656b6131307368656e227d/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=45'>46</a>\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mpymars\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39msoln2yaml\u001b[39;00m \u001b[39mimport\u001b[39;00m write\n\u001b[1;32m     <a href='vscode-notebook-cell://ssh-remote%2B7b22686f73744e616d65223a22457572656b6131307368656e227d/home/kf/projects_dir/Cantera_test/JetSurf_1_0/SL_reduction.ipynb#W0sdnNjb2RlLXJlbW90ZQ%3D%3D?line=46'>47</a>\u001b[0m write(reduced_gas, \u001b[39m'\u001b[39m\u001b[39mJetSurf_1_0.yaml\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mreduced_sl_49.yaml\u001b[39m\u001b[39m'\u001b[39m)\n",
      "\u001b[0;31mImportError\u001b[0m: attempted relative import with no known parent package"
     ]
    }
   ],
   "source": [
    "import cantera as ct\n",
    "import numpy as np\n",
    "\n",
    "gas = ct.Solution('JetSurf_1_0.yaml')\n",
    "gas.TP = 1000.0, 10 * ct.one_atm\n",
    "gas.set_equivalence_ratio(phi=1.0, fuel=\"NC7H16\", oxidizer={\"O2\": 1.0, \"N2\": 3.76})\n",
    "sparse_weight = np.load('sparse_weight.npy', allow_pickle=True)\n",
    "reduced_kenetics_index = np.where(sparse_weight > 9e-1)[0]\n",
    "\n",
    "species_in_selected_reactions = set([])\n",
    "for i in reduced_kenetics_index:\n",
    "    species_in_selected_reactions.update(gas.reaction(i).reactants.keys())\n",
    "    species_in_selected_reactions.update(gas.reaction(i).products.keys())\n",
    "species_list = list(species_in_selected_reactions) + ['AR', 'HE', 'N2']\n",
    "species = []\n",
    "for sp in gas.species():\n",
    "    if sp.name in species_list:\n",
    "        species.append(sp)\n",
    "\n",
    "species_idx_list = []\n",
    "for sp in gas.species():\n",
    "    if sp.name in species_list:\n",
    "        species_idx_list.append(gas.species_index(sp.name))\n",
    "\n",
    "reactions = []\n",
    "for rct in gas.reactions():\n",
    "    reac_indices = set(gas.species_index(species) for species in rct.reactants)\n",
    "    prod_indices = set(gas.species_index(species) for species in rct.products)\n",
    "\n",
    "    # Check if all reactants and products are exclusively from the selected species\n",
    "    if reac_indices.issubset(set(species_idx_list)) and prod_indices.issubset(set(species_idx_list)):\n",
    "        reactions.append(rct)\n",
    "\n",
    "print(f\"Number of species : {len(species)}.\")\n",
    "print(f\"Number of reactions : {len(reactions)}.\")\n",
    "\n",
    "print(len(species), len(reactions))\n",
    "\n",
    "reduced_gas = ct.Solution(\n",
    "    thermo='ideal-gas', \n",
    "    kinetics='gas',\n",
    "    species=species, \n",
    "    reactions=reactions,\n",
    ")\n",
    "\n",
    "from soln2yaml import write\n",
    "write(reduced_gas, 'JetSurf_1_0.yaml', 'reduced_sl_49.yaml')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
